﻿using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace LaoLuo
{
    public class MultiTypeObject
    {
        private readonly IEnumerable<dynamic> _objs;

        public MultiTypeObject(params dynamic[] objs)
            : this(objs.AsEnumerable())
        {

        }

        public MultiTypeObject(IEnumerable<dynamic> objs)
        {
            _objs = objs;
        }

        public T Call<T>(string action, params object[] parameters)
        {
            return (T)Call(action, parameters);
        }

        public object Call(string action, params object[] parameters)
        {
            foreach (object obj in _objs)
            {
                MethodInfo methodInfo = obj.GetType().GetMethod(action);
                if (methodInfo != null)
                {
                    object o = methodInfo.Invoke(obj, parameters);
                    return o;
                }
            }
            return null;
        }
    }
}
